<?php

namespace controller;

use request\Request as IndexRequest;
use message\AjaxMessage;
use request\Request;
use view\RenderIndex;
use controller\InputValidator;
use exceptions\InputValidationException;
use model\IndexQuery;
use session\UserSession;

class IndexController {
	private static $instance;
	private $controller;
	private function __construct() {
	}
	public static function run() {
		self::$instance = new IndexController ();
		self::$instance->handleRequest ();
	}
	private function handleRequest() {
		$request = new IndexRequest ();
		
		if (! ($request->isAjax ())) {
			$page = new RenderIndex ();
			$page->render ();
		}
		
		$this->validateInput ( $request );
		
		if ($request->getProperty ( 'action' ) == 'Register') {
			$email = $request->getProperty ( 'emailreg' );
			$username = $request->getProperty ( 'username' );
			$password = $request->getProperty ( 'password' );
			$repeatPassword = $request->getProperty ( 'passwordRepeat' );
			
			$this->register ( $email, $username, $password, $repeatPassword );
		} elseif ($request->getProperty ( 'action' ) == 'Login') {
			$email = $request->getProperty ( 'email' );
			$password = $request->getProperty ( 'password' );
			
			$this->login ( $email, $password );
		}
	}
	private function validateInput(Request $request) {
		try {
			if ($request->getProperty ( 'email' )) {
				$email = $request->getProperty ( 'email' );
				
				InputValidator::validateEmail ( $email );
			}
			if ($request->getProperty ( 'emailreg' )) {
				$email = $request->getProperty ( 'emailreg' );
				InputValidator::validateEmail ( $email );
				InputValidator::isEmailAvailable ( $email );
			}
			if ($request->getProperty ( 'password' )) {
				$password = $request->getProperty ( 'password' );
				
				InputValidator::validatePassword ( $password );
			}
			if ($request->getProperty ( 'username' )) {
				$username = $request->getProperty ( 'username' );
				
				InputValidator::isUserNameAvailable ( $username );
			}
		} catch ( InputValidationException $ive ) {
			AjaxMessage::sendErrorMessage ( $ive->getMessage () );
		}
	}
	private function register($email, $username, $password, $repeatPassword) {
		try {
			InputValidator::checkRepeatPassword ( $password, $repeatPassword );
			IndexQuery::registerUser ( $email, $password, $username );
		} catch ( InputValidationException $ive ) {
			AjaxMessage::sendErrorMessage ( $ive->getMessage () );
		} catch ( Exception $e ) {
			AjaxMessage::sendErrorMessage ( $e->getMessage () );
		}
	}
	private function login($email, $password) {
		try {
			
			if (IndexQuery::userExists ( $email, $password )) {
				$userInfo = IndexQuery::selectUser ( $email, $password );
				
				$user = new UserSession ( $userInfo ['player_id'], $userInfo ['username'] );
				session_start ();
				$_SESSION ['user'] = $user;
				header ( "Location: index.php?page=home" );
				die ();
			} else {
				AjaxMessage::sendErrorMessage ( "Invalid input" );
   }
   
   // session_start ();
  } catch ( Exception $e ) {
   AjaxMessage::sendErrorMessage ( $e->getMessage () );
  }
	}
}